﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace NMLX_Data_Management
{
    public partial class Fqianr : Form
    {
        private ProjectDetail projectDetail;
        private int projectid;
        private int programid;
        private string path;
        private string projectname;
        private string programname;
        private string qcr;

        public Fqianr()
        {
            InitializeComponent();
        }

        public Fqianr(ProjectDetail projectDetail, int projectid, int programid, string path, string projectname, string programname, string qcr)
        {
            this.projectDetail = projectDetail;
            this.projectid = projectid;
            this.programid = programid;
            this.path = path;
            this.projectname = projectname;
            this.programname = programname;
            this.qcr = qcr;
            InitializeComponent();
        }

        private void Fqianr_DragEnter(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            textBox1.Text = path;
        }

        private void Fqianr_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“nMLX_Data_MDataSet.User”中。您可以根据需要移动或删除它。
            this.userTableAdapter.Fill(this.nMLX_Data_MDataSet.t_User);
            label6.Text += "       "+this.path;
            label7.Text = this.projectname.ToString();
            label8.Text = this.programname.ToString();



        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }
        /// <summary>
        /// 签入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
           
            try
            {
                //如果上次签出和本次签入不是一个人
                if (comboBox1.Text.ToString()!=this.qcr&& this.qcr!="")
                {
                    DialogResult dr = MessageBox.Show("该程序上次由"+this.qcr+"签出，本次是否由"+comboBox1.Text+"签入", "提示", MessageBoxButtons.OKCancel);
                    if (dr == DialogResult.OK)
                    {
                        backgroundWorker1.RunWorkerAsync();
                    }
                    else if (dr == DialogResult.Cancel)
                    {
                        //用户选择取消的操作
                        this.Close();
                    }
                }
                else
                {
                    backgroundWorker1.RunWorkerAsync(); 
                }
               
               
               

                //if (NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql)>0)
                //{
                //    NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql1);
                //    MessageBox.Show("成功","保存成功");
                //    
                //}
                //else
                //{
                //    MessageBox.Show("失败");                    
                //}




            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

          

        }

        private void QRMethod()
        {

            try
            {
                //获取上传路径
                //完整路径
                string path1 = textBox1.Text.ToString();
                //文件所在目录
                string filepath = Path.GetDirectoryName(path1);
                //文件名包含后缀
                string filename = Path.GetFileName(path1);

                //程序签入
                var p = AppDomain.CurrentDomain.BaseDirectory;
                //删除旧程序
                FolderHelper.DelectDir(p + this.path);
                File.Copy(path1, p + this.path + "\\" + filename, true);
                //程序备份
                //Directory.CreateDirectory(this.path);
                var backpath = this.path;
                backpath = backpath.Substring(0, backpath.LastIndexOf("\\")) + "\\历史备份\\" + DateTime.Now.ToString("yyyy-MM-dd") + comboBox1.Text;
                //创建备份目录
                Directory.CreateDirectory(backpath);
                File.Copy(path1, p + backpath + "\\" + filename, true);
                //更新数据库信息
                string sql = string.Format("update ProjectDetail set qcr='',QCDate='', LastUpdateName='{0}',LastUpdateDate='{1}',UpdateLog='{2}' " +
                    "where ProjectID='{3}' and ProjectDetail={4}",
                    comboBox1.Text, DateTime.Now, textBox2.Text, this.projectid, this.programid);

                string sql1 = string.Format("insert into DetailRecord (username,datet,ProgramName,backpath,log,ProjectName,ProjectId,ProgramId,qcqr)" +
                    "values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}') ",
                    comboBox1.Text, DateTime.Now, filename, this.path, textBox2.Text, this.projectname, projectid, programid, "签入");

                ArrayList list = new ArrayList();
                list.Add(sql);
                list.Add(sql1);

                NMLX.DBUtility.DbHelperOleDb.ExecuteSqlTran(list);
                this.progressBar1.Value = 100;
                MessageBox.Show(this,"签入成功", "提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
                
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        private void Fqianr_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           
            this.progressBar1.Value = 50;
            QRMethod();
           

        }
    }
}
